package com.weimob.mcloud.wechatwork.customer.facade.impl.mall;

import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.bo.NodeBaseInfoBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.request.StoreQueryReq;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.response.StoreResponse;
import com.weimob.mcloud.wechatwork.core.adapter.service.MallAdapterService;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PageResponse;
import com.weimob.mcloud.wechatwork.customer.service.user.enums.VidTypeEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.mall.NodeSimpleBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.req.mall.StoreSimpleQueryReq;
import com.weimob.mcloud.wechatwork.customer.service.user.service.MallClient;
import com.weimob.mcloud.wechatwork.customer.third.BosThird;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/10/30 5:35 下午
 */
@Service
public class MallClientImpl implements MallClient {

    @Autowired
    private MallAdapterService mallAdapterService;

    @Autowired
    private BosThird bosThird;

    @Override
    public SoaResponse<PageResponse<NodeSimpleBO>, ?> queryStorePage(StoreSimpleQueryReq storeSimpleQueryReq) {

        //todo 参数校验
        BasicInfo basicInfo = storeSimpleQueryReq.getBasicInfo();
        StoreQueryReq storeQueryReq = new StoreQueryReq();

        //List<ProductInstanceDTO> productInstanceDTOS = bosThird.queryProductInstanceDTO(basicInfo.getBosId(), Lists.newArrayList(145L));
        //TODO 先查询全部吧
        storeQueryReq.setBosId(basicInfo.getBosId());
        //storeQueryReq.setVids(basicInfo.getVid());
        storeQueryReq.setPageSize(storeSimpleQueryReq.getPageSize());
        storeQueryReq.setPageNum(storeSimpleQueryReq.getPageNum());
        storeQueryReq.setVidName(storeSimpleQueryReq.getRetailStoreName());
        if (CollectionUtils.isEmpty(storeSimpleQueryReq.getVidTypes())) {
            //默认查询门店
            storeQueryReq.setVidTypes(Lists.newArrayList(VidTypeEnum.VID_SHOP.getCode()));
        } else {
            storeQueryReq.setVidTypes(storeSimpleQueryReq.getVidTypes());
        }
        //1是可用
        //storeQueryReq.setVidStatus(1);

        PageResponse<NodeSimpleBO> pageInfoResponse = new PageResponse<>();
        pageInfoResponse.setPageNum(storeSimpleQueryReq.getPageNum());
        pageInfoResponse.setPageSize(storeSimpleQueryReq.getPageSize());

        //参数校验
        SoaResponse<com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.page.PageInfoResponse<StoreResponse>, ?> pageInfoResponseSoaResponse = mallAdapterService.queryStorePage(storeQueryReq);
        com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.page.PageInfoResponse<StoreResponse> storeResponsePageInfoResponse = SoaUtil.unpack(pageInfoResponseSoaResponse);

        if (Objects.isNull(storeResponsePageInfoResponse)) {
            pageInfoResponse.setTotalCount(0L);
            pageInfoResponse.setTotalPage(0);
            pageInfoResponse.setResults(Lists.newArrayList());
            return SoaUtil.ok(pageInfoResponse);
        }

        pageInfoResponse.setTotalCount(storeResponsePageInfoResponse.getTotalCount());
        pageInfoResponse.setTotalPage(storeResponsePageInfoResponse.getTotalPage());
        pageInfoResponse.setResults(covert2NodeSimpleBO(storeResponsePageInfoResponse.getDataList()));

        return SoaUtil.ok(pageInfoResponse);
    }

    private List<NodeSimpleBO> covert2NodeSimpleBO(List<StoreResponse> storeResponseList) {
        if (CollectionUtils.isEmpty(storeResponseList)) {
            return new ArrayList<>();
        }

        List<NodeSimpleBO> nodeSimpleBOList = new ArrayList<>();
        for (StoreResponse storeResponse : storeResponseList) {
            NodeBaseInfoBO nodeBaseInfoBO = storeResponse.getNodeBaseInfoBO();
            if (Objects.isNull(nodeBaseInfoBO)) {
                continue;
            }
            NodeSimpleBO nodeSimpleBO = new NodeSimpleBO();
            nodeSimpleBO.setBosId(nodeBaseInfoBO.getBosId());
            nodeSimpleBO.setMerchantId(nodeBaseInfoBO.getMerchantId());
            nodeSimpleBO.setVid(nodeBaseInfoBO.getVid());
            nodeSimpleBO.setVidName(nodeBaseInfoBO.getVidName());
            nodeSimpleBO.setVidStatus(nodeBaseInfoBO.getVidStatus());
            nodeSimpleBO.setVidTypes(nodeBaseInfoBO.getVidTypes());
            nodeSimpleBOList.add(nodeSimpleBO);
        }
        return nodeSimpleBOList;
    }
}
